<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019-03-18
  Time: 11:16
--%>

<%@ page import="org.apache.commons.lang.StringEscapeUtils; sysUtil.JsoupUtil" contentType="text/html;charset=UTF-8" %>
<%@ taglib  uri ="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html>
<html>
<head>
    <title>Form填报</title>
    <script type="text/javascript" src=".././static/js/public-head.js"></script>
</head>

<body>

<form class="layui-form" lay-filter="itemForm-dom">
    <input type="hidden" name="widgetPanelId" value="${widgetPanelId}">
    <input type="hidden" name="0ption" value="${formValue?"edit":"add"}">
    <input type="hidden" name="0ldValue" value="${formValue}">

    <div class="layui-form-item">
        <g:each in="${(List<xdTool.XdTableColCfg>)xdTableColCfg}" var="item">
            <g:if test="${formValue}"><%-- 判断是新增还是编辑,如果是新增，则主键字段无需展示--%>
                <div class="layui-inline">
                    <label class="layui-form-label">${item.title?:item.field}</label>
                    <div class="layui-input-inline" xd-bing="${item.field}"  >
                         <%-- 如果绑定有值来源,则直接生成下拉框组件--%>
                        <input name="${item.field}" id="${item.field}" value="" class="layui-input"
                               lay-verify="${item.dataType == "Number" ? "number":(item.dataType == "Date"?"required":"" )}"
                               autocomplete="off"
                            ${ item.primaryKey?"readonly":""} >
                    </div>
                </div>
            </g:if>
            <g:else>

                <g:if test="${item.primaryKey}">

                </g:if>
                <g:else>
                    <div class="layui-inline">
                        <label class="layui-form-label">${item.title?:item.field}</label>
                        <div class="layui-input-inline" xd-bing="${item.field}"  >
                            <input name="${item.field}" id="${item.field}" value="" class="layui-input"
                                   lay-verify="${item.dataType == "Number" ? "number":(item.dataType == "Date"?"required":"" )}"
                                   autocomplete="off" >
                        </div>
                    </div>
                </g:else>
            </g:else>

            <g:if test="${item.paramId}"> <%-- 如果绑定有值来源,则直接生成下拉框组件--%>
                <script type="text/javascript">
                    $('div[xd-bing=${item.field}]').html(commonUtil.parseServerHtml(' ${sysUtil.JsoupUtil.getTianBaoBingdingHtml(item.id)} '))
                </script>
            </g:if>
        </g:each>
    </div>

    <div class="layui-form-item layui-layout-admin">
        <div class="layui-input-block">
            <div class="layui-footer" style="left: 0;">
                <button class="layui-btn" lay-submit="" lay-filter="itemForm-bt">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>

</form>

</body>
</html>
<script type="text/javascript">

    layui.config({
        base: basePath+'/layui/lay/modules/'
    }).extend({
        formSelects: 'formSelects-v4'
    }).use([ 'form','laydate','formSelects' ], function() {
        var $ = layui.$
        var layer = layui.layer
        var form = layui.form
        var laydate = layui.laydate;
        var formSelects = layui.formSelects;
        var jsonValue = JSON.parse(commonUtil.parseServerHtml('${formValue?:"{}"}') )

        <g:each in="${(List<xdTool.XdTableColCfg>)xdTableColCfg}" var="item">
            <g:if test="${item.paramId}">//如果绑定参数不为空,则生成select组件
                <g:set var="formSelect" value="${xdTool.XdWidgetSelect.get(item.paramId)}" />
                <g:if test="${formSelect && formSelect.serverFlag == "true"}">//如果select是远程模式,则config-select，否则render
                        formSelects.data('${formSelect.name}', 'server', {//远程根据 keyword 查询
                            type:"post",
                            data:{id:${formSelect.id},initData:jsonValue.${item.field} },
                            searchUrl:basePath+"/xdWidgetSelect/selectView",
                            success: function(id, url, searchVal, result){           //使用远程方式的error回调
                                $('div[xd-bing=${item.field}]').width('${formSelect.width}')
                            }
                        });
                </g:if>
                <g:else>
                        formSelects.render('${formSelect.name}',{init: [jsonValue.${item.field}]});
                </g:else>
            </g:if>
            <g:elseif test="${item.dataType=="Date"}" >
                    laydate.render({
                        elem: '#${item.field}',
                        type:"datetime",
                        value: new Date(jsonValue.${item.field} == null ? "${new java.util.Date().format("yyyy-MM-dd 00:00:00")}":jsonValue.${item.field})
                })
            </g:elseif>
            <g:else>
                form.val("itemForm-dom",{"${item.field}":jsonValue.${item.field}})
            </g:else>
        </g:each>

        form.on("submit(itemForm-bt)",function(obj){
            $.post(basePath+"/xdWidgetPanel/save",obj.field,function(rst){
                if(rst.code !=0){
                    layer.alert(rst.msg,{icon:2})
                }else{
                    layer.alert(rst.msg,{icon:1},function(index){
                        commonUtil.closeLayerFrame()
                    })
                }
            },"json")
            return false
        })
    });



</script>